<?php

namespace App\Inventor\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\PatentTransactionRepository;
use App\Services\PatentTransactionService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;

class PatentTransactionController extends Controller
{
    protected $title = '已转化专利';
    public $repository;
    public $service;

    public function __construct(PatentTransactionRepository $repository, PatentTransactionService $service)
    {
        $this->repository = $repository;
        $this->service = $service;
    }

    public function index(Content $content)
    {
        return $content
            ->title($this->title)
            ->body($this->grid());
    }

    protected function grid()
    {
        $user = Admin::user();
        $grid = $this->repository->grid();
        $grid->model()->where(function ($query) use ($user) {
            $query->where('user_id', $user->id)->orWhereHas('patentApply',function($query) use($user){
                $username = $user['name'];
                $query->where('inventor', 'like', "%$username%");
            });
        })->orderBy('id', 'desc');
        return $this->service->gridColumn($grid, 0);
    }
}
